/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package azure;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.Scanner;

/**
 * Static class to log errors in the same datafile
 * @author Gargravarr
 * @date Aug 23, 2011
 */
public class ErrorLogger
{

    public static final String LOGFILE = "error.log";

    /**
     * Log an error message in the text file
     * @param jClass The class the error originated in
     * @param lineNumber The line number
     * @param localisedMessage Originally a localized message, now just a plain String representation of the message
     * @return 
     */
    public static boolean logErrorMessage(String jClass, int lineNumber, String localisedMessage)
    {
        try
        {
            OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(LOGFILE, true), "UTF-8");
            sw.write("Time: " + Calendar.getInstance().getTime().toString() + "\n");
            sw.write("Class: " + jClass + "\n");
            sw.write("Line number: " + lineNumber + "\n");
            sw.write("Message: " + localisedMessage + "\n");
            sw.write("=============================");
            sw.close();
            return true;
        }
        catch (Exception se)
        {
            return false;
        }
    }

    public static String loadErrorLog()
    {
        try
        {
            StringBuilder sb = new StringBuilder();
            Scanner sc = new Scanner(new File(LOGFILE));
            while (sc.hasNextLine())
            {
                sb.append(sc.nextLine());
                sb.append("\n");
            }
            sc.close();
            return sb.toString();
        }
        catch (FileNotFoundException fnf)
        {
            return "Log File Not Found! Not good!";
        }
    }
}
